package com.demo2do.fms.admin.service.data.impl;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Service;

import com.demo2do.core.entity.exception.BusinessException;
import com.demo2do.core.entity.page.Page;
import com.demo2do.core.persistence.annotation.Dao;
import com.demo2do.core.persistence.support.QueryFilter;
import com.demo2do.core.persistence.support.QueryOrder;
import com.demo2do.fms.admin.dao.data.FacilityDistanceDao;
import com.demo2do.fms.admin.entity.report.data.FacilityDistanceReportSearch;
import com.demo2do.fms.admin.handler.support.FmsJdbcSupport;
import com.demo2do.fms.admin.service.DataServiceImpl;
import com.demo2do.fms.admin.service.data.FacilityDistanceService;
import com.demo2do.fms.entity.authority.Organization;
import com.demo2do.fms.entity.data.business.FacilityDistance;
import com.demo2do.fms.entity.data.business.LoadPlace;

/**
 * 
 * @author Ysy
 *
 */
@Service("facilityDistanceService")
public class FacilityDistanceServiceImpl extends DataServiceImpl<FacilityDistance> implements FacilityDistanceService{

    @Dao
    private FacilityDistanceDao facilityDistanceDao;
    
    @Autowired
    private FmsJdbcSupport fmsJdbcSupport;
    /*
     * (non-Javadoc)
     * @see com.demo2do.fms.admin.service.FacilityDistanceService#count(java.util.List)
     */
    public int count(List<Long> orgIds) {
        
        //if orgIds is null, return 0
        if(CollectionUtils.isEmpty(orgIds))
            return 0;
        
        QueryFilter queryFilter = new QueryFilter()
                                  .addSentence("organization.id IN (:orgIds)", "orgIds", orgIds);
        
        return super.count(FacilityDistance.class, queryFilter);
    }
    
    /*
     * (non-Javadoc)
     * @see com.demo2do.fms.admin.service.FacilityDistanceService#list(java.util.List, com.demo2do.core.entity.page.Page)
     */
    public List<FacilityDistance> list(List<Long> orgIds, Page page) {
        
        QueryFilter queryFilter = new QueryFilter()
                                  .addSentence("organization.id IN (:orgIds)", "orgIds", orgIds);
        
        QueryOrder queryOrder = new QueryOrder().add("organization.name", "ASC")
                                                .add("fromLoadPlace.name", "ASC");
        
        return super.listByPage(FacilityDistance.class, queryFilter, queryOrder, page);
    }
    
    /*
     * (non-Javadoc)
     * @see com.demo2do.fms.admin.service.data.FacilityDistanceService#list(com.demo2do.fms.entity.organization.Organization, com.demo2do.fms.entity.data.business.LoadPlace, com.demo2do.fms.entity.data.business.LoadPlace)
     */
    public List<FacilityDistance> list(Organization organization, LoadPlace fromLoadPlace, LoadPlace toLoadPlace) {
        return facilityDistanceDao.list(organization.getId() ,fromLoadPlace.getId(), toLoadPlace.getId());
    }
    
    /*
     * (non-Javadoc)
     * @see com.demo2do.fms.admin.service.FacilityDistanceService#create(com.demo2do.fms.entity.data.business.FacilityDistance)
     */
    public void create(FacilityDistance facilityDistance, Long operator) {
        
        Date now = new Date();
        
        List<FacilityDistance> facilityDistances = facilityDistanceDao.list(facilityDistance.getOrganization().getId(), facilityDistance.getFromLoadPlace().getId(), facilityDistance.getToLoadPlace().getId());
        
        if (!facilityDistances.isEmpty()) {
            throw BusinessException.newInstance("500", "行驶公里数重复");
        }
        
        genericDaoSupport.save(facilityDistance.initialize(operator, now));
    }
    
    /*
     * (non-Javadoc)
     * @see com.demo2do.fms.admin.service.data.FacilityDistanceService#edit(java.lang.Long, com.demo2do.fms.entity.data.business.FacilityDistance)
     */
    public void edit(Long operator , FacilityDistance facilityDistance) {
        genericDaoSupport.update(facilityDistance.modify().operator(operator, new Date()));
    }

    
    /* (non-Javadoc)
     * @see com.demo2do.fms.admin.service.data.FacilityDistanceService#exportAll(com.demo2do.fms.admin.entity.report.data.FacilityDistanceReportSearch)
     */
    public List<Map<String, Object>> exportAll(FacilityDistanceReportSearch search) {
        String sentence = search.buildSentenceTemplate();
        Map<String, Object> parameters = search.buildParameters();
        RowMapper<Map<String, Object>> rowMapper = search.buildRowMapper();
        return fmsJdbcSupport.queryForList(sentence, parameters, rowMapper);
    }

}
